//
//  UIColor+Util.m
//  iosapp
//
//  Created by chenhaoxiang on 14-10-18.
//  Copyright (c) 2014年 oschina. All rights reserved.
//

#import "UIColor+Util.h"

@implementation UIColor (Util)

#pragma mark - Hex

+ (UIColor *)colorWithHex:(int)hexValue alpha:(CGFloat)alpha
{
    return [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0
                           green:((float)((hexValue & 0xFF00) >> 8))/255.0
                            blue:((float)(hexValue & 0xFF))/255.0
                           alpha:alpha];
}

+ (UIColor *)colorWithHex:(int)hexValue
{
    return [UIColor colorWithHex:hexValue alpha:1.0];
}


#pragma mark - theme colors

+ (UIColor *)themeColor
{
    return [UIColor colorWithRed:235.0/255 green:235.0/255 blue:243.0/255 alpha:1.0];
}

+ (UIColor *)themeViewBackgroundColor
{
    return [UIColor colorWithHex:0xF4F4F4];
//    return [UIColor redColor];
}

+ (UIColor*)themeToolBarColor{
    return [UIColor colorWithHex:0xffffff alpha:0.1];
}

+ (UIColor*)themeHighLightColor{
    return [UIColor colorWithHex:0xfe573d alpha:1];
}

+ (UIColor*)themeHighLightColorDark{
    return [UIColor colorWithHex:0xF13E32 alpha:1];
}

+ (UIColor*)themeHighLightColorH{
    return [UIColor colorWithHex:0xfe573d alpha:0.5f];
}

+ (UIColor *)themeBlueColor {
    return [UIColor colorWithHex:0x5a96e6 alpha:1];
}

+ (UIColor *)themeGrayColorWithBorder {
    return [UIColor colorWithHex:0xf1e7e6 alpha:1];
}

+ (UIColor *)themeGrayColorWithHint {
    return [UIColor colorWithHex:0xa9a9a9 alpha:1];
}

+ (UIColor *)themeGrayColorWithContent {
    return [UIColor colorWithHex:0x333333 alpha:1];
}

+ (UIColor *)themeGrayColorWithBackground {
    return [UIColor colorWithHex:0xf4f4f4 alpha:1];
}

+ (UIColor *)themeGrayColorWithDisableCell {
    return [UIColor colorWithHex:0xFAF4F5 alpha:1];
}

+ (UIColor *)themeLightYellowColorWithBackground {
    return [UIColor colorWithHex:0xfdffe4 alpha:1];
}

+ (UIColor *)themeYellowColorWithProgressView {
    return [UIColor colorWithHex:0xffc773 alpha:1];
}

+ (UIColor *)themeGrayColorWithProgressTrack {
    return [UIColor colorWithHex:0xf5ecea alpha:1];
}

+ (UIColor *)themeYellowColor {
    return [UIColor colorWithHex:0xFFE96A alpha:1];
}

+ (UIColor *)nameColor
{
    return [UIColor colorWithHex:0xfe573d alpha:1];
}

+ (UIColor *)tabTitleColor {
    return [self blackColor];
}

+ (UIColor *)navTitleColor {
    return [UIColor colorWithHex:0xffffff];
}

+ (UIColor *)navBackgroundColor {
    // return [UIColor colorWithHex:0xF7F8F8];
    return [UIColor themeHighLightColor];
}

- (UIImage *)imageWithSize:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [self CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (NSArray*)levelColorsWithBeginColor:(UIColor*)beginColor endColor:(UIColor*)endColor addedLevel:(int)addedLevel{
    
    CGFloat begR, begG, begB;
    CGFloat endR, endG, endB;
    CGFloat deltaR, deltaG, deltaB;

    [beginColor getRed:&begR green:&begG blue:&begB alpha:0];
    [endColor getRed:&endR green:&endG blue:&endB alpha:0];
    
    begR *= 255;
    begG *= 255;
    begB *= 255;
    
    endR *= 255;
    endG *= 255;
    endB *= 255;

    deltaR = (endR - begR)/(addedLevel + 1);
    deltaG = (endG - begG)/(addedLevel + 1);
    deltaB = (endB - begB)/(addedLevel + 1);
    
    NSMutableArray* arrayLevels = [NSMutableArray array];
    [arrayLevels addObject:beginColor];
    
    for (int i = 0; i< addedLevel; i++) {
        CGFloat r = begR + (i+1)*deltaR;
        CGFloat g = begG + (i+1)*deltaG;
        CGFloat b = begB + (i+1)*deltaB;
        UIColor* color = RGB(r, g, b);
        [arrayLevels addObject:color];
    }
    
    [arrayLevels addObject:endColor];

    return arrayLevels;
}

@end
